import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        in.nextLine();
        String[] arr = new String[N];
        for (int i = 0; i < N; i++) {
            arr[i] = in.nextLine();
        }
        int[] dp = new int[M];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = in.nextInt();
        }
        int[][] nums = new int[M][10];
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < 10; j++) {
                nums[i][j] = in.nextInt();
            }
        }


        int Xiao = in.nextInt();
        int[] Ji = new int[Xiao];
        for (int i = 0; i < Xiao; i++) {
                 Ji[i] = in.nextInt();
                 if (IsWen(Ji[i],dp)){
                     System.out.println("?");
                 }
                 int total = Soul(Ji[i],dp,nums);
                    if (total == -1){
                        System.out.println("?");
                    }
                    else{
                        System.out.println(arr[total - 1]);
                    }
        }
    }


    public static boolean IsWen(int a,int[] arr){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==a/10){
                return false;
            }
        }
        return true;
    }
    public static int Soul(int a,int[] dp,int[][] nums){
        int C = a/10;
        int x = 0;
        for (int i = 0; i < dp.length; i++) {
            if (dp[i] == C){
                x = i;
                break;
            }
        }
        int CC = a%10;
        return nums[x][CC];
    }

}
